import Axios from 'axios'
// import router from '@/router'
// import store from '@/store'
import NProgress from 'nprogress'
const service = Axios.create({
  baseURL: (import.meta.env.VITE_API_URL) || undefined,
  timeout: 50000,
})

service.interceptors.request.use(
  (config) => {
    NProgress.start()
    return config
  },
  (error) => {
    return error;
  }
)

service.interceptors.response.use(
  (response) => {
    const { data } = response
    // const { code, msg } = data

    // if (code !== 0) {
    //   return Promise.reject(new Error(msg || 'Error'))
    // }

    NProgress.done()
    return Promise.resolve(data)
  },
  (error) => {
    NProgress.done()
    // const response = Object.assign({}, error.response)
    // response && Message.error(CodeMessage[response.status] || '系统异常')
    return Promise.reject(error)
  }
)

const request = (config) => {
  return new Promise((resolve, reject) => {
    service
      .request(config)
      .then((res) => resolve(res.data))
      .catch((err) => reject(err))
  })
}

request.get = (url, params) => {
  request({
    method: 'get',
    url,
    params,
  })
}

request.post = (url, params) => {
  request({
    method: 'post',
    url,
    data: params,
  })
}

request.delete = (url, params) => {
  request({
    method: 'delete',
    url,
    params,
  })
}

request.put = (url, params) => {
  request({
    method: 'put',
    url,
    data: params,
  })

}
request.patch = (url, params) => {
  request({
    method: 'patch',
    url,
    data: params,
  })
}

export default request
